package com.example.goodcolor;

import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Game extends Activity {

	View topLevelLayout;
	View layout;
	ImageView logo;
	ImageView triangle;
	ImageView replay;
	TextView point;
	int i=0;
	Handler handler;
	private RotateAnimation animation;
	private Handler myHandler;
	private Runnable myRunnable;

	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		topLevelLayout = findViewById(R.id.top_layout);
		layout=findViewById(R.id.activity_game);
		logo = (ImageView)findViewById(R.id.imageViewlogo);
		triangle = (ImageView)findViewById(R.id.imageView1triangle);
		replay = (ImageView)findViewById(R.id.imageViewreplay);
		point = (TextView)findViewById(R.id.textView1point);
		
		if (isFirstTime()) {
        	topLevelLayout.setVisibility(View.INVISIBLE);
        }
		
		if(topLevelLayout.getVisibility() == View.VISIBLE){
			startDemo();
		}else{
			startGame();
		}
		
		
	    
		triangle.setOnClickListener(new View.OnClickListener(){
		    public void onClick(View v) {
		    	Activity context;
				Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
				vibe.vibrate(150);
		    }
		});
		
		
		replay.setOnClickListener(new View.OnClickListener(){
		    public void onClick(View v) {
			    myHandler.removeCallbacks(myRunnable);
				point.setText(0+"");
		    	ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK );
				logo.setColorFilter(filter);
		    	MyCount timerCount = new MyCount(1 * 1000, 1000);
				timerCount.start();
		    	animation = new RotateAnimation(0f, 360f, replay.getWidth()/2, replay.getHeight()/2); 
		    	animation.setInterpolator(new LinearInterpolator());
		        animation.setRepeatCount(Animation.INFINITE);
		        animation.setDuration(800);
		        replay.startAnimation(animation);
		    }
		});
		
	}

	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game, menu);
		return true;
	}*/
	

	 public class MyCount extends CountDownTimer {
	      public MyCount(long millisInFuture, long countDownInterval) {
	        super(millisInFuture, countDownInterval);
	      }

	      @Override
	      public void onFinish() {
	       animation.cancel();
		   startGame();
	      }

	      @Override
	      public void onTick(long millisUntilFinished) {
	        //some script here 
	      }   
	} 
	
	
	public void startGame(){
		myHandler = new Handler();
		
		final int rgbColor[][] = { 
				{212,73,55},
				{135,199,103},
				{51,122,184},
				{210,222,82},
				{194,62,62},
				{217,126,209}
		};
		
		Random r = new Random();
		int nb = r.nextInt(5 - 0 + 0) + 0;
		final int color = Color.rgb(rgbColor[nb][0],rgbColor[nb][1],rgbColor[nb][2]);
		
		myRunnable = new Runnable() {
			@Override
			public void run() {
				if(i==0){
					ColorFilter filter = new LightingColorFilter( color, color);
					logo.setColorFilter(filter);
					//point.setText(Integer.parseInt(point.getText().toString())+1+"");
					i=1;
				}else{
					ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK );
					logo.setColorFilter(filter);
					//triangle.setColorFilter(filter);
					i=0;
				}
			    myHandler.postDelayed(this,700);
			    }
			};

		    myHandler.postDelayed(myRunnable,700); // on redemande toute les 500ms
	}
	
	
	
	public void startDemo(){
		myHandler = new Handler();
		myRunnable = new Runnable() {
			@Override
			public void run() {
				if(i==0){
					ColorFilter filter = new LightingColorFilter( Color.RED, Color.RED );
					logo.setColorFilter(filter);
					triangle.setColorFilter(filter);
					point.setText(Integer.parseInt(point.getText().toString())+1+"");
					i=1;
				}else{
					ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK );
					logo.setColorFilter(filter);
					triangle.setColorFilter(filter);
					i=0;
				}
			    myHandler.postDelayed(this,500);
			    }
			};

		    myHandler.postDelayed(myRunnable,500); // on redemande toute les 500ms
	}
	
	
	public boolean isFirstTime()
	{
	    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
	    boolean ranBefore = preferences.getBoolean("RanBefore", false);
	    if (!ranBefore) {
	    
	        SharedPreferences.Editor editor = preferences.edit();
	        editor.putBoolean("RanBefore", true);
	        editor.commit();
			topLevelLayout.setVisibility(View.VISIBLE);
	        topLevelLayout.setOnTouchListener(new View.OnTouchListener(){
	
		@Override
		public boolean onTouch(View v, MotionEvent event) {
		topLevelLayout.setVisibility(View.INVISIBLE);
		myHandler.removeCallbacks(myRunnable);
		point.setText(0+"");
		startGame();
		return false;
		}
		            
		            });
		}
		return ranBefore;
	}
	

}
